<title>区域设置</title>
<!--当前位置开始-->
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <script type="text/html" template lay-done="layui.data.updateMainBreadcrumb();">
        </script>
    </div>
</div>

<style>
    /* 左树 */
    #areaTreeBar { padding: 10px 15px; border: 1px solid #e6e6e6; background-color: #f2f2f2; }
    #areaTree { border: 1px solid #e6e6e6; padding: 10px 5px; overflow: auto; height: -webkit-calc(100vh - 158px); height: -moz-calc(100vh - 158px); height: calc(100vh - 158px); }
    .layui-tree-entry .layui-tree-txt { padding: 0 5px; border: 1px transparent solid; text-decoration: none !important; }
    .layui-tree-entry.ew-tree-click .layui-tree-txt { background-color: #fff3e0; border: 1px #FFE6B0 solid; }
    /* 右表搜索表单 */
    #areaTbSearchForm .layui-form-label { box-sizing: border-box !important; width: 90px !important; }
    #areaTbSearchForm .layui-input-block { margin-left: 90px !important; }
    /* 主管标识 */
    td .leader-flag { background: #52c41a; color: #fff; font-size: 12px; line-height: 18px; position: absolute; padding: 0 15px; right: -15px; top: 2px; -webkit-transform: rotate(45deg); transform: rotate(45deg); -webkit-transform-origin: center; transform-origin: center; }
</style>
<!-- 正文开始 -->
<script type="text/html" template lay-type="Post" lay-url="{{ layui.setter.apiUrl }}Api/CoreCmsArea/GetIndex" lay-done="layui.data.done(d);">

    <div class="layui-fluid" style="padding-bottom: 0;">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md2">
                <div class="layui-card">
                    <div class="layui-card-body" style="padding: 10px;">
                        <!-- 左树 -->
                        <div id="areaTree"></div>
                    </div>
                </div>
            </div>
            <div class="layui-col-md10">
                <div class="layui-card">
                    <div class="layui-card-body" style="padding: 10px;">
                        <!-- 数据表格 -->
                        <table id="areaTable" lay-filter="areaTable"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="areaTbBar">
    <!--{{#  if(d.depth <3 ){ }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add">添加下级</a>
    {{#  } }}-->
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>



<script type="text/html" id="LAY-app-CoreCmsArea-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addData"><i class="layui-icon layui-icon-add-1"></i>添加下级</button>
    </div>
</script>


<script>
    var indexData;
    var debug = layui.setter.debug;
    var checkId;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d); }

        indexData = d.data;
        layui.use(['index', 'table', 'laydate', 'util', 'tree', 'dropdown', 'tableX', 'coreHelper', 'treeTable'],
            function () {
                var $ = layui.$
                    , admin = layui.admin
                    , table = layui.table
                    , form = layui.form
                    , laydate = layui.laydate
                    , setter = layui.setter
                    , coreHelper = layui.coreHelper
                    , util = layui.util
                    , ztree = layui.ztree
                    , treeTable = layui.treeTable
                    , tableX = layui.tableX
                    , view = layui.view;
                var tree = layui.tree;
                var selObj, treeData;  // 左树选中数据

                /* 渲染树形 */
                function renderTree() {
                    coreHelper.Post('Api/CoreCmsArea/GetAllList', null, function (res) {
                        if (res.code !== 0) {
                            return layer.msg(res.msg, { icon: 2 });
                        }
                        for (var i = 0; i < res.data.length; i++) {
                            res.data[i].title = res.data[i].name;
                            res.data[i].id = res.data[i].id;
                            res.data[i].spread = false;
                        }
                        treeData = layui.treeTable.pidToChildren(res.data, 'id', 'parentId');
                        tree.render({
                            elem: '#areaTree',
                            onlyIconControl: false,
                            data: treeData,
                            click: function (obj) {
                                selObj = obj;
                                $('#areaTree').find('.ew-tree-click').removeClass('ew-tree-click');
                                $(obj.elem).children('.layui-tree-entry').addClass('ew-tree-click');
                                checkId = obj.data.id;
                                insTb.reload({
                                    where: { parentId: obj.data.id },
                                    url: layui.setter.apiUrl + 'Api/CoreCmsArea/GetPageList',
                                    method: 'post'
                                });

                                //if (obj.data.depth < 3) {
                                //    checkId = obj.data.id;
                                //    console.log(checkId);
                                //    insTb.reload({
                                //        where: { parentId: obj.data.id },
                                //        url: layui.setter.apiUrl + 'Api/CoreCmsArea/GetPageList',
                                //        method: 'post'
                                //    });
                                //}
                            }
                        });
                        $('#areaTree').find('.layui-tree-entry:first>.layui-tree-main>.layui-tree-txt').trigger('click');
                    });
                }
                renderTree();


                /* 渲染表格 */
                var insTb = tableX.render({
                    elem: '#areaTable',
                    data: [],
                    toolbar: '#LAY-app-CoreCmsArea-toolbar',
                    height: 'full-135',
                    page: false,
                    cellMinWidth: 100,
                    cols: [[
                        { type: 'numbers', fixed: 'left' },
                        { field: 'id', title: '地区ID', width: 80, sort: false },
                        { field: 'parentId', title: '父级ID', sort: false },
                        { field: 'depth', title: '地区深度', sort: false },
                        { field: 'name', title: '地区名称', sort: false },
                        { field: 'postalCode', title: '邮编', sort: false },
                        { field: 'sort', title: '地区排序', sort: false },
                        {
                            title: '操作', toolbar: '#areaTbBar', unresize: true,
                            align: 'center', fixed: 'right', width: 100
                        }
                    ]],
                    done: function () {
                        var $flag = $('#areaTable+div .leader-flag');
                        $flag.parent().parent().append($flag);
                    }
                });

                //监听工具条
                table.on('tool(areaTable)', function (obj) {
                    if (obj.event === 'del') {
                        doDelete(obj);
                    } else if (obj.event === 'edit') {
                        doEdit(obj)
                    }
                });

                //头工具栏事件
                table.on('toolbar(areaTable)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                    case 'addData':
                        doCreate(checkId);
                        break;
                    };
                });

                //执行创建操作
                function doCreate(checkId) {
                    coreHelper.Post("Api/CoreCmsArea/GetCreate", { id: checkId }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '创建数据',
                                area: ['350px', '350px'],
                                id: 'LAY-popup-CoreCmsArea-create',
                                success: function (layero, index) {
                                    view(this.id).render('shop/area/create', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsArea-createForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsArea/DoCreate", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        renderTree();
                                                        //layui.table.reloadData('areaTable'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    });
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-CoreCmsArea-createForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行编辑操作
                function doEdit(obj) {
                    coreHelper.Post("Api/CoreCmsArea/GetEdit", { id: obj.data.id }, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '编辑数据',
                                area: ['350px', '350px'],
                                id: 'LAY-popup-CoreCmsArea-edit',
                                success: function (layero, index) {
                                    view(this.id).render('shop/area/edit', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsArea-editForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsArea/DoEdit", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        renderTree();
                                                        //layui.table.reloadData('areaTable'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    })
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-CoreCmsArea-editForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行单个删除
                function doDelete(obj) {
                    layer.confirm('确定删除吗？删除后将无法恢复。', function (index) {
                        coreHelper.Post("Api/CoreCmsArea/DoDelete", { id: obj.data.id }, function (e) {
                            if (debug) { console.log(e); } //开启调试返回数据
                            renderTree();
                            //table.reloadData('areaTable');
                            layer.msg(e.msg);
                        });
                    });
                }

            });
    };
</script>